package com.codrim.cs.data.server.dao.realtime;

import com.codrim.cs.data.rpc.dto.param.OfferReportField;
import com.codrim.cs.data.server.dao.param.RealTimeDataSearchParam;

import java.util.Arrays;
import java.util.function.BiConsumer;
import java.util.function.Function;


/**
 * Created by Liang.Zhuge on 09/08/2017.
 */
public enum RealTimeGroupByField {
    Affiliate("Affiliate", RealTimeKey::getAffId, RealTimeDataSearchParam::getAffiliateId, RealTimeKey::setAffId),
    Offer("Offer", RealTimeKey::getOfferId,
            RealTimeDataSearchParam::getOfferId, RealTimeKey::setOfferId),
    Advertiser("Advertiser", RealTimeKey::getAdvId,
            RealTimeDataSearchParam::getAdvertiserId, RealTimeKey::setAdvId);

    public final String field;
    public final Function<RealTimeKey, Integer> getValueFromRealTimeKey;
    public final Function<RealTimeDataSearchParam, Integer> getValueFromSearchParam;
    public final BiConsumer<RealTimeKey, Integer> setValue;

    RealTimeGroupByField(String field,
            Function<RealTimeKey, Integer> getValueFromRealTimeKey,
            Function<RealTimeDataSearchParam, Integer> getValueFromSearchParam,
            BiConsumer<RealTimeKey, Integer> setValue) {
        this.field = field;
        this.getValueFromRealTimeKey = getValueFromRealTimeKey;
        this.getValueFromSearchParam = getValueFromSearchParam;
        this.setValue = setValue;
    }

    public static RealTimeGroupByField of(OfferReportField field) {
        return Arrays.stream(RealTimeGroupByField.values())
                .filter(item -> field.isGroupBy() && item.field.equals(field.getField().name()))
                .findFirst()
                .map(item -> item)
                .orElse(null);
    }
}
